package com.by.service.impl;

import com.by.bean.*;
import com.by.crud.BaseServiceImpl;
import com.by.ex.BusinessException;
import com.by.mapper.PaymentMapper;
import com.by.mapper.SupplierContactMapper;
import com.by.mapper.SupplierMapper;
import com.by.mapper.UserMapper;
import com.by.query.SupplierSave;
import com.by.query.SupplierUpdate;
import com.by.service.SupplierService;
import com.by.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author :LYJ
 * @version : 1.0
 * @date : 2025/2/17 20:16
 * @desc :
 * @since : JDK17
 */
@Service
public class SupplierServiceImpl extends BaseServiceImpl<Supplier, SupplierMapper> implements SupplierService {
    @Autowired
    UserMapper userMapper;
    @Lazy
    @Autowired
    SupplierService supplierService;

    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    PaymentMapper paymentMapper;
    @Autowired
    SupplierContactMapper supplierContactMapper;


    /**
     * 保存供应商、供应商联系人信息
     *
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveSupplier(SupplierSave supplierSave) {
        // 获取 Supplier 实体
        Supplier supplier = supplierSave.getSupplier();
        SupplierContact contact = supplierSave.getSupplierContact();
        Payment payment = supplierSave.getPayment();

        // 检查用户是否存在
        int c = userMapper.queryUserByUserName(supplier.getAccount());
        if (c > 0) {
            throw new BusinessException("该账号已存在！");
        }

        // 保存用户
        User user = new User(supplier.getAccount(), supplier.getPassword(), UserType.SUPPLIER_USER);
        int rs = userMapper.save(user);
        Assert.isZero(rs, "供应商用户保存失败");

        // 设置供应商 ID
        supplier.setId(user.getId());

        // 保存供应商基本信息
        int res = supplierMapper.save(supplier);
        Assert.isZero(res, "基本信息保存失败");

        // 设置联系人的供应商 ID
        contact.setSupplier(supplier.getId());
        //获取保存好的用户的账户,并将其保存到供应商联系人中
        contact.setAccount(supplier.getAccount());

        // 保存供应商联系人信息
        int res2 = supplierContactMapper.save(contact);
        Assert.isZero(res2, "联系人信息保存失败");

        // 设置付款信息的供应商 ID
        payment.setSupplier(supplier.getId());

        // 保存供应商付款信息
        int res3 = paymentMapper.save(payment);
        Assert.isZero(res3, "货款信息保存失败");

        return res;
    }

    /**
     * 删除供应商
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Serializable id) {
        return super.delete(id);
    }

    /**
     * 根据id更新供应商
     *
     * @param entity
     * @return
     */
    @Override
    public int update(Supplier entity) {
        //确保id存在
        Assert.isNull(entity.getId(), "id不能为空");
        //根据id获取数据库中的供应商信息
        Supplier supplier = supplierMapper.findById(entity.getId());
        if (supplier == null) {
            throw new BusinessException("供应商不存在");
        }
        //更新字段
        if (entity.getName() != null) {
            supplier.setName(entity.getName());
        }
        if (entity.getArea() != null) {
            supplier.setArea(entity.getArea());
        }
        if (entity.getAddress() != null) {
            supplier.setAddress(entity.getAddress());
        }
        //执行更新
        return super.update(entity);
    }

    /**
     * 根据map查询供应商
     *
     * @param map 查询包含的条件
     * @return
     */
    public PageBean queryByMap(Map map) {
        return super.queryByMap(map);
    }

    /**
     * 连表查询全部的信息
     *
     * @param map
     * @return
     */
    public List queryByMap2(Map map) {
        return supplierMapper.queryByMap2(map);
    }


    /**
     * 更新供应商信息
     *
     * @param supplierUpdate
     */
    @Transactional
    @Override
    public void updateSupplier(SupplierUpdate supplierUpdate) {
        // 更新供应商基本信息
        supplierMapper.updateSupplierBasicInfo(
                supplierUpdate.getId(),
                supplierUpdate.getName(),
                supplierUpdate.getFication(),
                supplierUpdate.getQuotation()
        );

        // 更新供应商联系信息
        supplierMapper.updateSupplierContactInfo(
                supplierUpdate.getId(),
                supplierUpdate.getContact_name(),
                supplierUpdate.getSale_number()
        );
    }
}
